package com.wuhan.template.module.home.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.wuhan.template.R;
import com.wuhan.template.base.BaseActivity;
import com.wuhan.template.module.home.adapter.HotSearchBaseAdapter;
import com.wuhan.template.module.home.entity.HotWordEntity;
import com.wuhan.template.utils.EventMessage;
import com.wuhan.template.utils.OkHttpUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/13 0013.
 */
public class SearchListActivity extends BaseActivity {

    private String keyWord;
    private String encode;

    @BindView(R.id.search_list_pullList)
    protected PullToRefreshListView listView;

    @BindView(R.id.shuaxuan_text)
    protected TextView shaixuan;

    @BindView(R.id.shaixuan_layout)
    protected LinearLayout linearLayout;
    private List<HotWordEntity> hotWordLists;
    private String urlType;

    @BindView(R.id.search_back)
    protected ImageView imageBack;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_list;
    }

    @BindView(R.id.radioGroup)
    protected RadioGroup group;


    private String url1 = "http://mobile.appwuhan.com/zswuhan/search_list.php?appid=16&appkey=rFUm5PYocCj6e1h0m03t3WarVJcMV98c&device_token=2d375c69c8348fcfa06e3cde5753339c&_member_id=&version=3.1&app_version=3.1";
    private String url2 = "&column_id=&time=0&search_text=";
    //String url = url1+"&bundle_id=news" + url2+ encode + "&offset=0";
    private boolean isGone = true;

    @Override
    protected void initViews() {
        linearLayout.setVisibility(View.GONE);

        shaixuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isGone) {

                    linearLayout.setVisibility(View.VISIBLE);
                    isGone = false;
                }
                linearLayout.setVisibility(View.GONE);
                isGone = true;
            }
        });


        try {
            keyWord = getIntent().getStringExtra("keyWord");
            encode = URLEncoder.encode(keyWord, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                hotWordLists.clear();
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });

        imageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SearchListActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    @BindView(R.id.quanbu)
    protected RadioButton button;

    @Override
    protected void loadData() {
       // OkHttpUtils.get(url1 + "&bundle_id=news" + url2 + encode + "&offset=0", null);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.quanbu:
                        urlType = "&bundle_id=news";
                        OkHttpUtils.get(url1 + urlType + url2 + encode + "&offset=0", null);
                        break;
                    case R.id.wengao:
                        urlType = "&bundle_id=news";
                        OkHttpUtils.get(url1 + urlType + url2 + encode + "&offset=0", null);
                        break;
                    case R.id.tuji:
                        urlType = "&bundle_id=tuji";
                        OkHttpUtils.get(url1 + urlType + url2 + encode + "&offset=0", null);
                        break;
                    case R.id.shipin:
                        urlType = "&bundle_id=livmedia";
                        OkHttpUtils.get(url1 + urlType + url2 + encode + "&offset=0", null);
                        break;
                }
            }
        });
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if (message.type.equals(url1 + urlType + url2 + encode + "&offset=0")) {
            Gson gson = new Gson();
            HotWordEntity[] hotWordEntities = gson.fromJson(message.result, HotWordEntity[].class);
            hotWordLists = new ArrayList<>();
            hotWordLists.addAll(Arrays.asList(hotWordEntities));

            HotSearchBaseAdapter baseAdapter = new HotSearchBaseAdapter(this, hotWordLists);
            listView.setAdapter(baseAdapter);

            baseAdapter.notifyDataSetChanged();
            listView.onRefreshComplete();
        }

    }
}
